package com.purong.purong.activity;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Administrator on 2016/7/18.
 */
public class SplashScreenActivity extends BaseActivity {
    private long startTime;
    private long minTime = 2500;
    private ImageView slogan,logo;

    @Override
    public void initView() {
        slogan=(ImageView)findViewById(R.id.slogan);
        logo=(ImageView)findViewById(R.id.logo);
        int logoSize= (int)(CommonUtil.getScreenWidth(this)/4.2);
        logo.setLayoutParams(new LinearLayout.LayoutParams(logoSize,logoSize));
        int sloganWidth=(int)(CommonUtil.getScreenWidth(this)/1.6);
        int sloganHeight=(int)(sloganWidth/3.55);
        slogan.setLayoutParams(new LinearLayout.LayoutParams(sloganWidth,sloganHeight));
    }

    @Override
    public void initData() {
        startTime = System.currentTimeMillis();
        //初始化应用
        if (SharePreUtil.getInstance(this).getToken()==null||SharePreUtil.getInstance(this).getToken().equals("")){
            if (!reInit(null)) {
                interMain();
            }
        }else {
            interMain();
        }
    }

    @Override
    public int getRootViewId() {
        return R.layout.activity_splash;
    }

    //定时跳转
    public void interMain() {
        long time = System.currentTimeMillis() - startTime;
        if (time < minTime) {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (SharePreUtil.getInstance(SplashScreenActivity.this).getShowIntroduce()) {
                        Intent intent = new Intent(SplashScreenActivity.this, GuideActivity.class);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    SplashScreenActivity.this.finish();
                }
            }, minTime - time);
        } else {
            Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(intent);
            SplashScreenActivity.this.finish();
        }
    }


    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy()){
            return;
        }
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.INITIAL)) {
            interMain();
        }
//        else if (responseInfo.url.equals(Contant.LOGIN)) {
//            if (responseInfo.isRequestOk){
//                try{
//                    LoginBean bean = JSON.parseObject(responseInfo.content, LoginBean.class);
//                    if (bean != null) {
//                        bean.setPwd(responseInfo.params.get("user_pwd"));
//                        bean.save(this);
//                    }
//                    interMain();
//                }catch (Exception e){
//                    LoginBean.remove(this);
//                    interMain();
//                }
//            }else {
//                LoginBean.remove(this);
//                interMain();
//            }
//
//
//        }


    }
}
